<template>
  <el-dialog
    :title="!dataForm.id ? '受理' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="20%"
    >
      <el-form-item label="选择基础配置" prop="sdnConfigId">
        <el-select v-model="dataForm.sdnConfigId" placeholder="请选择">
          <el-option v-for="(sdnConfig, index) in sdnConfigs" :label="sdnConfig.name" :value="sdnConfig.id"
                     :key="index"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="选择策略" prop="policyConfigId">
        <el-select v-model="dataForm.policyConfigId" placeholder="请选择">
          <el-option v-for="(policy, index) in policys" :label="policy.name" :value="policy.id"
                     :key='index'></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data() {
      return {
        visible: false,
        ids: [],
        isAdd: 1,
        dataForm: {},
        sdnConfigs: [],
        policys: [],
        dataRule: {
          sdnConfigId: [
            {required: true, message: "请选择基础配置", trigger: "blur"}
          ],
          policyConfigId: [
            {required: true, message: "请选择策略", trigger: "blur"}
          ]
        }
      };
    },
    methods: {
      init(ids, domainId, isAdd, row) {
        this.dataForm = {}
        this.ids = ids;
        this.visible = true;
        this.isAdd = isAdd;
        if (this.isAdd === 2) {
          this.dataForm.sdnConfigId = row.sdnConfigId;
          this.dataForm.policyConfigId = row.policyConfigId;
        }

        this.$http({
          url: this.$http.adornEndogenousUrl("/sdnconfig/list"),
          method: "get",
          params: this.$http.adornParams({
            domainId: domainId,
            page: this.pageIndex,
            limit: this.pageSize,
            key: this.dataForm.key
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.sdnConfigs = data.page.list;
          }
        });

        this.$http({
          url: this.$http.adornEndogenousUrl("/policyconfig/list"),
          method: "get",
          params: this.$http.adornParams({
            domainId: domainId,
            page: this.pageIndex,
            limit: this.pageSize,
            key: this.dataForm.key
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.policys = data.page.list;
          }
        });
      },
      // 表单提交
      dataFormSubmit() {
        this.$refs["dataForm"].validate(valid => {
          if (valid) {
            this.$http({
              url: this.$http.adornEndogenousUrl(
                `/uservmtrustedrel/${this.isAdd == 1 ? "save" : "update"}`
              ),
              method: "post",
              data: this.$http.adornData({
                idents: this.ids,
                sdnConfigId: this.dataForm.sdnConfigId,
                policyConfigId: this.dataForm.policyConfigId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: "操作成功",
                  type: "success",
                  duration: 1500,
                  onClose: () => {
                    this.visible = false;
                    this.$emit("refreshDataList");
                  }
                });
              } else {
                this.$message.error(data.msg);
              }
            });
          }
        });
      }
    }
  };
</script>
